package com.unhz.taigu.service.impl;

import com.unhz.generator.dao.UserDictMapper;
import com.unhz.generator.pojo.UserDict;
import com.unhz.generator.utils.IsNumeric;
import com.unhz.generator.utils.Page;
import com.unhz.taigu.service.UserDictService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: 沈佳庆
 * @Description: 用户信息数据字典
 * @Date: Created in 下午 7:00 2018/1/23 0023
 * @Modified By:
 */
@Service
public class UserDictServiceImpl implements UserDictService {
    static final int PAGENUM = 1;
    private Logger logger = LoggerFactory.getLogger(UserDictServiceImpl.class);

    @Autowired
    private UserDictMapper userDictMapper;


    @Override
    public Page<UserDict> selectPageUserDict(String pageNo, Integer pageSize, String keywords) {
        int pageNum = PAGENUM;
        boolean numok = IsNumeric.isNumeric(pageNo);
        if(numok){
            if(pageNo !=null && !"".equals(pageNo)){
                pageNum = Integer.parseInt((pageNo));
            }
        }
        int totalRecords = userDictMapper.getTotalRecordsByKeywords(keywords);        //获取记录总条数
        Page page = new Page(pageNum,totalRecords);
        page.setPageSize(pageSize);
        Map<String ,Object> map = new HashMap<String,Object>();
        map.put("startIndex",page.getStartIndex());
        map.put("pageSize",page.getPageSize());
        map.put("keywords",keywords);
        //获取总记录数
        List<UserDict> records = userDictMapper.findUserDictMap(map);
        page.setRecords(records);
        page.setUri("/userDict/findAllUserDict");
        return page;
    }

    @Override
    public List<UserDict> findAllType() {
        return userDictMapper.findAllType();
    }

    @Override
    public String insertUserDict(UserDict userDict) {
        Integer type = userDict.getType();
        int sortMax = userDictMapper.selectOrderByMax(type);
        int sort = sortMax + 1;
        userDict.setSort(sort);
        int count = userDictMapper.insertSelective(userDict);
        if(count > 0){
            return "success";
        }
        return "false";
    }

    @Override
    public UserDict findDictById(Integer dictId) {
        return userDictMapper.findDictById(dictId);
    }

    @Override
    public void deleteById(Integer dictId) {
        userDictMapper.deleteById(dictId);
    }

    @Override
    public List<UserDict> findJobType() {
        return userDictMapper.findJobType();
    }

    @Override
    public List<UserDict> findRangeType() {
        return userDictMapper.findRangeType();
    }

}
